package com.hlyj.http.base.tool.lib_hlyj_base.customview

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import android.view.animation.LinearInterpolator
import com.cszy.mfjx.lib_hlyj_base.R


class LinearProgressBar @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    private val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        color = Color.parseColor("#FFFFFF")
        style = Paint.Style.FILL
    }

    private var progress = 0f
    private val animator = ValueAnimator()
    private var currentSegment = 0
    private val segments = listOf(
        // 格式: (startProgress, endProgress, duration, delayAfter)
        Segment(0f, 0.5f, 3000L, 1000L),   // 0-50% 3秒，停1秒
        Segment(0.5f, 0.6f, 1000L, 500L),  // 50-60% 1秒，停0.5秒
        Segment(0.6f, 0.7f, 1000L, 500L),  // 60-70% 1秒，停0.5秒
        Segment(0.7f, 0.9f, 1500L, 500L),  // 70-90% 1.5秒，停0.5秒
        Segment(0.9f, 0.95f, 1000L, 1000L),// 90-95% 1秒，停1秒
        Segment(0.95f, 1f, 1000L, 0L)      // 95-100% 1秒
    )

    private data class Segment(
        val start: Float,
        val end: Float,
        val duration: Long,
        val delayAfter: Long
    )

    init {
        context.obtainStyledAttributes(attrs, R.styleable.InfiniteLinearProgressBar).apply {
            paint.color = getColor(R.styleable.InfiniteLinearProgressBar_progressColor, paint.color)
            recycle()
        }

        animator.interpolator = LinearInterpolator()
        animator.addUpdateListener {
            progress = it.animatedValue as Float
            invalidate()
        }
    }

    override fun onDraw(canvas: Canvas) {
        canvas.drawColor(Color.WHITE)
        canvas.drawRect(
            0f,
            0f,
            width * progress,
            height.toFloat(),
            paint
        )
    }

    fun start() {
        if (animator.isRunning) {
            animator.cancel()
        }
        currentSegment = 0
        progress = 0f
        // 初始延迟1秒
        postDelayed({
            startNextSegment()
        }, 1000)
    }

    private fun startNextSegment() {
        if (currentSegment >= segments.size) return

        val segment = segments[currentSegment]
        animator.setFloatValues(segment.start, segment.end)
        animator.duration = segment.duration

        animator.removeAllListeners()
        animator.addUpdateListener {
            progress = it.animatedValue as Float
            invalidate()
        }

        animator.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator) {
                if (segment.delayAfter > 0) {
                    postDelayed({
                        currentSegment++
                        startNextSegment()
                    }, segment.delayAfter)
                } else {
                    currentSegment++
                    startNextSegment()
                }
            }
        })

        animator.start()
    }

    fun stop() {
        animator.cancel()
        currentSegment = segments.size // 标记所有段已完成
    }

    fun pause() {
        animator.pause()
    }

    fun resume() {
        animator.resume()
    }

    fun setProgress(progress: Float) {
        this.progress = progress.coerceIn(0f, 1f)
        invalidate()
    }

    fun getProgress(): Float = progress

    override fun onDetachedFromWindow() {
        stop()
        super.onDetachedFromWindow()
    }
}